package model

import (
	"go-task/dao"
	"gorm.io/gorm"
)

type TimeTaskHis struct {
	Id              int64  `json:"id" gorm:"primary_key;autoIncrement"`
	TaskId          string `json:"taskId"`
	TaskName        string `json:"taskName"`
	TaskGroup       string `json:"taskGroup"`
	TaskType        string `json:"taskType"` // 任务类型：onetime-运行一次 repeat-重复执行
	CronExpr        string `json:"cronExpr"`
	SubType         string `json:"subType"` // 订阅类型：SHARED-共享 EXCLUSIVE-独占
	TaskData        string `json:"taskData"`
	TaskExtendParam string `json:"taskExtendParam"`
	ExecStatus      int    `json:"execStatus"`
	SuccessNum      int    `json:"successNum"`
	FailNum         int    `json:"failNum"`
	CreateTime      string `json:"createTime"`
	UpdateTime      string `json:"updateTime"`
}

func (TimeTaskHis) TableName() string {
	return "time_task_his"
}

func (t TimeTaskHis) AddTaskHis(taskHis *TimeTaskHis) error {
	return dao.DB.Create(taskHis).Error
}

func (t TimeTaskHis) UpdateHis(id int64, taskId string, execStatus int) error {
	db := dao.DB.Model(t)
	db = db.Where("id = ? and task_id = ?", id, taskId)
	param := map[string]interface{}{
		"exec_status": execStatus,
	}
	if execStatus == 1 {
		param["success_num"] = gorm.Expr("success_num + ?", 1)
	}
	if execStatus == 2 {
		param["fail_num"] = gorm.Expr("fail_num + ?", 1)
	}
	return db.Updates(param).Error
}
